Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added onconnectioncreate module option #2096

Merged
merged 5 commits into from
Apr 12, 2024

Conversation

prateekkathal
Copy link
Contributor

@prateekkathal prateekkathal commented Mar 20, 2024

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #1290

// THIS DOES NOT WORK
MongooseModule.forRootAsync({
  imports: [MongoConfigModule],
  useFactory: async (mongoConfigService: MongoConfigService) => {
    return {
      ...mongoConfigService.mongoConfig,
      connectionFactory: (connection: Connection) => {
        connection.on('connected', () => console.log('connected'));
        connection.on('open', () => console.log('open'));
        connection.on('disconnected', () => console.log('disconnected'));
        connection.on('reconnected', () => console.log('reconnected'));
        connection.on('disconnecting', () => console.log('disconnecting'));

        return connection;
      },
    };
  },
  inject: [MongoConfigService],
}),

What is the new behavior?

According to Mongoose Documentation, the event listeners should be added immediately after createConnection. By adding this onConnectionCreate function as a module option, we can bypass the limits of the connectionFactory function.

MongooseModule.forRootAsync({
  imports: [MongoConfigModule],
  useFactory: async (mongoConfigService: MongoConfigService) => {
    return {
      ...mongoConfigService.mongoConfig,
      // Once connection is established
      connectionFactory: (connection: Connection) => {
        connection.plugin(require('mongoose-autopopulate'));

        return connection;
      },
      // Before connection is established and returned as Promise.
      onConnectionCreate: (connection: Connection) => {
        connection.on('connected', () => console.log('connected'));
        connection.on('open', () => console.log('open'));
        connection.on('disconnected', () => console.log('disconnected'));
        connection.on('reconnected', () => console.log('reconnected'));
        connection.on('disconnecting', () => console.log('disconnecting'));

        return connection;
      },
    };
  },
  inject: [MongoConfigService],
}),

The same will work for forRootAsync().

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I can create the Main Docs PR if changes are approved.

@kamilmysliwiec kamilmysliwiec merged commit 56ac9b4 into nestjs:master Apr 12, 2024
1 check passed
@kamilmysliwiec
Copy link
Member

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants